package org.lsms.servlet;

/**
 * servlet used to assign paper(chair and member,more than one assignments
 * @author ZhanXueliang
 * 2010.8.31
 */

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lsms.bean.User;
import org.lsms.dao.impl.DaoImpl;
import org.lsms.mail.MailSenderInfo;
import org.lsms.mail.SimpleMailSender;

public class paper_assign1_all extends HttpServlet {

	
	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			int member_id = Integer.parseInt(request.getParameter("member_id"));
//			System.out.println("The member id is "+member_id);	
			String id = request.getParameter("paper_id");
//			System.out.println(id);
			String [] ids = id.split(",");
			int paper_num = ids.length;
//			for(int i=0;i<ids.length;i++){
//				System.out.println(ids[i]);
//			}
			
			DaoImpl impl = new DaoImpl();
			User user = impl.get_user_info(member_id);
			String loginname = user.getLoginname();
			String firstname = user.getFirstname();
			String middlename = user.getMiddlename();
			String lastname = user.getLastname();
			
			String email = user.getEmail();
			MailSenderInfo mailInfo = new MailSenderInfo();
			mailInfo.init(mailInfo);
			mailInfo.setToAddress(email);
			
			mailInfo.setSubject("LSMS & ICSEE 2010 Paper Assign");
			
			String content = "Dear Mr/Mrs " + firstname+" "+middlename+" "+lastname+'\n'+'\n';
			String letter_content = impl.get_mail_content(3);
			content +=letter_content;
			String ps = "\n\nPS:\nYour Loginname:"+loginname+".\nif you have forgotten your password, " +
					"you can click the \"forget password?\" button to reset it.";
			content +=ps;
			mailInfo.setContent(content);
			
			
			for(int j=0;j<ids.length;j++){
				SimpleMailSender sms = new SimpleMailSender();
				int paper_id = Integer.parseInt(ids[j]);
//				mailInfo.setContent(text);
//				mailInfo.setSubject(subject);
				if(impl.paper_assign1(paper_id, member_id) && sms.sendTextMail(mailInfo)){
					continue;
				}else{
					String info = "Problem occurs when assign paper "+paper_id+" to PC member!\n" +
							"Please try again later.";
					response.sendRedirect("chair/chair_show_info.jsp?chair_info="+info);
				}
			}
			String info = "paper assign successfully!";
			response.sendRedirect("chair/chair_show_info.jsp?chair_info="+info);
	}

	
	public void init() throws ServletException {
		
	}

}
